package hut.game.unit;

import hut.game.table.BanCo;
import hut.game.table.VanCo;
import android.widget.ImageView;

public class Tinh extends QuanCo {
	public Tinh() {
		super();
	}

	@Override
	public void KhoiTao(int phe, String name, String thutu, int stt,
			boolean khoa, int x, int y) {
		super.KhoiTao(phe, name, thutu, stt, khoa, x, y);
	}

	// Danh cho Computer
	private int diem = 2;

	public int getDiem() {
		if (this.TrangThai != 0) {
//			if (this.Hang == 0 || this.Hang == 9)
//				diem += 45;
//			else if (this.Hang == 4 || this.Hang == 5)
//				diem += 42;
//			else if (this.Hang == 2 || this.Hang == 7) {
//				if (this.Cot == 4)
//					diem += 48;
//				else
//					diem += 43;
//			}else 
			if(this.TrangThai == 1)
				diem = 20;
		} else
			diem = 0;
		return diem;
	}

	@Override
	public int KiemTra(int i, int j) {
		boolean turn = false;
		if (TrangThai == 2) {
			if (Phe == 0) {
				if (i > 4 || i < 0)
					turn = false;
				if (i >= 0 && i <= 4 && j >= 0 && j <= 8)
					if ((i == Hang - 2 && j == Cot - 2 && BanCo.ViTri[Hang - 1][Cot - 1].Trong == true)
							|| (i == Hang - 2 && j == Cot + 2 && BanCo.ViTri[Hang - 1][Cot + 1].Trong == true)
							|| (i == Hang + 2 && j == Cot - 2 && BanCo.ViTri[Hang + 1][Cot - 1].Trong == true)
							|| (i == Hang + 2 && j == Cot + 2 && BanCo.ViTri[Hang + 1][Cot + 1].Trong == true)) {
						if (BanCo.ViTri[i][j].Trong == true)
							turn = true;
						if (BanCo.ViTri[i][j].Trong == false)
							if (BanCo.ViTri[i][j].Phe != this.Phe)
								turn = true;
					}
			}
			if (Phe == 1) {
				if (i > 9 || i < 5)
					turn = false;
				if (i >= 5 && i <= 9 && j >= 0 && j <= 8)
					if ((i == Hang - 2 && j == Cot - 2 && BanCo.ViTri[Hang - 1][Cot - 1].Trong == true)
							|| (i == Hang - 2 && j == Cot + 2 && BanCo.ViTri[Hang - 1][Cot + 1].Trong == true)
							|| (i == Hang + 2 && j == Cot - 2 && BanCo.ViTri[Hang + 1][Cot - 1].Trong == true)
							|| (i == Hang + 2 && j == Cot + 2 && BanCo.ViTri[Hang + 1][Cot + 1].Trong == true)) {
						if (BanCo.ViTri[i][j].Trong == true)
							turn = true;
						if (BanCo.ViTri[i][j].Trong == false)
							if (BanCo.ViTri[i][j].Phe != this.Phe)
								turn = true;
					}
			}
		}else{
			if ((i == Hang - 2 && j == Cot - 2 && BanCo.ViTri[Hang - 1][Cot - 1].Trong == true)
					|| (i == Hang - 2 && j == Cot + 2 && BanCo.ViTri[Hang - 1][Cot + 1].Trong == true)
					|| (i == Hang + 2 && j == Cot - 2 && BanCo.ViTri[Hang + 1][Cot - 1].Trong == true)
					|| (i == Hang + 2 && j == Cot + 2 && BanCo.ViTri[Hang + 1][Cot + 1].Trong == true)) {
				if (BanCo.ViTri[i][j].Trong == true)
					turn = true;
				if (BanCo.ViTri[i][j].Trong == false)
					if (BanCo.ViTri[i][j].Phe != this.Phe)
						turn = true;
			}
		}
		// Kiểm tra lộ mặt tướng
		if (VanCo.state) {
			if (VanCo.n1.qTuong.Cot == VanCo.n2.qTuong.Cot
					&& Cot == VanCo.n2.qTuong.Cot) {
				int ct = 0;
				if (j != Cot) {
					VanCo.OCoTrong(Hang, Cot);
					for (int t = VanCo.n1.qTuong.Hang + 1; t < VanCo.n2.qTuong.Hang; t++) {
						if (BanCo.ViTri[t][Cot].Trong == false)
							ct++;
					}
					if (ct == 0)
						turn = false;
					BanCo.ViTri[Hang][Cot].Trong = false;
					BanCo.ViTri[Hang][Cot].Ten = this.Ten;
					BanCo.ViTri[Hang][Cot].Phe = this.Phe;
					BanCo.ViTri[Hang][Cot].ThuTu = this.ThuTu;
				}
			}
		} else {
			if (VanCo.n1.qTuong.Cot == VanCo.computer.qTuong.Cot
					&& Cot == VanCo.n1.qTuong.Cot) {
				int ct = 0;
				if (j != Cot) {
					VanCo.OCoTrong(Hang, Cot);
					for (int t = VanCo.computer.qTuong.Hang + 1; t < VanCo.n1.qTuong.Hang; t++) {
						if (BanCo.ViTri[t][Cot].Trong == false)
							ct++;
					}
					if (ct == 0)
						turn = false;
					BanCo.ViTri[Hang][Cot].Trong = false;
					BanCo.ViTri[Hang][Cot].Ten = this.Ten;
					BanCo.ViTri[Hang][Cot].Phe = this.Phe;
					BanCo.ViTri[Hang][Cot].ThuTu = this.ThuTu;
				}
			}
		}
		if (turn)
			return 1;
		else
			return 0;
	}

	@Override
	public int TuongAnToan(int i, int j) {
		boolean turn = true;
		// Kiểm tra tướng an toàn
		if (turn == true) {
			int ho, co;
			QuanCo temp = null;
			temp = new QuanCo();

			ho = this.Hang;
			co = this.Cot;
			if (VanCo.state) {
				if (BanCo.ViTri[i][j].Trong == false) {
					if (BanCo.ViTri[i][j].Phe == VanCo.n1.Phe) {
						for (int k = 0; k < VanCo.n1.dsQuanCo.size(); k++) {
							if (VanCo.n1.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten
									&& VanCo.n1.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
								temp = VanCo.n1.dsQuanCo.get(k);
						}
					} else {
						for (int k = 0; k < VanCo.n2.dsQuanCo.size(); k++) {
							if (VanCo.n2.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten
									&& VanCo.n2.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
								temp = VanCo.n2.dsQuanCo.get(k);
						}
					}
				}
			} else {
				if (BanCo.ViTri[i][j].Trong == false) {
					if (BanCo.ViTri[i][j].Phe == VanCo.computer.Phe) {
						for (int k = 0; k < VanCo.computer.dsQuanCo.size(); k++) {
							if (VanCo.computer.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten
									&& VanCo.computer.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
								temp = VanCo.computer.dsQuanCo.get(k);
						}
					} else {
						for (int k = 0; k < VanCo.n1.dsQuanCo.size(); k++) {
							if (VanCo.n1.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten
									&& VanCo.n1.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
								temp = VanCo.n1.dsQuanCo.get(k);
						}
					}
				}
			}
			// Giả sử quân cờ được đi để kiểm tra Tướng mình có bị chiếu ko
			VanCo.OCoTrong(Hang, Cot);
			BanCo.ViTri[i][j].Trong = false;
			BanCo.ViTri[i][j].Phe = Phe;
			BanCo.ViTri[i][j].Ten = Ten;
			BanCo.ViTri[i][j].ThuTu = ThuTu;
			this.Hang = i;
			this.Cot = j;
			if (temp.Phe != 2) {
				temp.TrangThai = 0;
				// temp.imgUnit.setVisibility(ImageView.INVISIBLE);
			}
			// Kiểm tra
			if (VanCo.state) {
				if (this.Phe == VanCo.n1.Phe) {
					if (VanCo.ChieuTuong(VanCo.n1.qTuong) == true)
						turn = false;
				} else {
					if (VanCo.ChieuTuong(VanCo.n2.qTuong) == true)
						turn = false;
				}
			} else {
				if (this.Phe == VanCo.n1.Phe) {
					if (VanCo.ChieuTuong(VanCo.n1.qTuong) == true)
						turn = false;
				} else {
					if (VanCo.ChieuTuong(VanCo.computer.qTuong) == true)
						turn = false;
				}
			}
			// Trả lại những gì đã giả sử ở trên ^^!
			this.Hang = ho;
			this.Cot = co;
			VanCo.OCoTrong(i, j);
			BanCo.ViTri[ho][co].Trong = false;
			BanCo.ViTri[ho][co].Phe = Phe;
			BanCo.ViTri[ho][co].Ten = Ten;
			BanCo.ViTri[ho][co].ThuTu = ThuTu;
			if (temp.Phe != 2) {
				if(temp.isFirst && temp.Ten != "tuong")
					temp.TrangThai = 2;
				else
					temp.TrangThai = 1;
				temp.imgUnit.setVisibility(ImageView.VISIBLE);
				BanCo.ViTri[i][j].Trong = false;
				BanCo.ViTri[i][j].Ten = temp.Ten;
				BanCo.ViTri[i][j].Phe = temp.Phe;
				BanCo.ViTri[i][j].ThuTu = temp.ThuTu;
				BanCo.ViTri[i][j].TrangThai = temp.TrangThai;
			}
		}
		// Trả về kết quả
		if (turn)
			return 1;
		else
			return 0;
	}

}
